package com.zhangtai.exception;




import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.security.auth.login.AccountExpiredException;
import javax.servlet.http.HttpServletRequest;

import java.sql.SQLException;
import java.util.Date;

/**
 * @Classname BExceptionHandler
 * @Description 自定义异常处理
 * @Author 李号东 lihaodongmail@163.com
 * @Date 2019-03-29 13:23
 * @Version 1.0
 */
@Slf4j
@RestControllerAdvice
public class RExceptionHandler {
    @Autowired
    private SendErr sendErr;


    /**
     * 处理自定义异常
     */
    @ExceptionHandler(RException.class)
    public R handlerException(RException e, HttpServletRequest request) {

        log.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
        String time = DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN);
        sendErr.send(request,e);
        return R.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e, HttpServletRequest request) {

        log.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
        sendErr.send(request,e);
        return R.error(404, "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) {
        log.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);

        sendErr.send(request,e);
        return R.error(300, "数据库中已存在该记录");
    }

    @ExceptionHandler(AccessDeniedException.class)
    public R handleAuthorizationException(AccessDeniedException e, HttpServletRequest request) {
//        log.info("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
//        sendErr.send(e);
        return R.error(501, "没有权限，请联系管理员授权");
    }

    @ExceptionHandler(AccountExpiredException.class)
    public R handleAccountExpiredException(AccountExpiredException e, HttpServletRequest request) {
        log.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
        sendErr.send(request,e);
        return R.error(e.getMessage());
    }





    @ExceptionHandler(Exception.class)
    public R handleException(Exception e, HttpServletRequest request) {
        log.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
        sendErr.send(request,e);
        return R.error(e.getMessage());
    }

    @ExceptionHandler(SQLException.class)
    public R handlerSqlException(SQLException e, HttpServletRequest request) {
        log.error("URL : {}, error : {}", request.getRequestURL(), e.getMessage(),e);
        sendErr.send(request,e);
        return R.error(e.getMessage());
    }


}
